package cn.zero.api.controller;

import java.util.concurrent.Callable;
import java.util.function.Supplier;

import org.springframework.web.bind.annotation.ExceptionHandler;

import cn.zero.http.dto.ResultEntity;

public class BaseAPIController {
	
	@ExceptionHandler(value = RuntimeException.class)
	public Object handleRuntimeException(Exception e) {
		return failure(e.getMessage());
	}
	
	protected Object success() {
		return new ResultEntity();
	}
	
	protected Object success(Object data) {
		return new ResultEntity(0, data);
	}
	
	protected Callable<ResultEntity> asynSuccess(Supplier<Object> supplier) {
		return () -> {
			return new ResultEntity(0, supplier.get());
		};
	}

	protected Object failure(String msg) {
		return new ResultEntity(500, msg);
	}
	
}
